package me.zhengjie.modules.equipment.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.customer.domain.TCustomerInfo;
import me.zhengjie.modules.customer.service.TCustomerInfoService;
import me.zhengjie.modules.customer.service.dto.TCustomerInfoDTO;
import me.zhengjie.modules.equipment.domain.TEqptModel;
import me.zhengjie.modules.equipment.domain.TEquipment;
import me.zhengjie.modules.equipment.service.TEqptModelService;
import me.zhengjie.modules.equipment.service.TEquipmentService;
import me.zhengjie.modules.equipment.service.dto.TEqptModelQueryCriteria;
import me.zhengjie.modules.equipment.service.dto.TEquipmentQueryCriteria;
import me.zhengjie.utils.HttpResponseUtils;
import me.zhengjie.utils.StringUtils;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author sw
* @date 2019-06-29
*/
@RestController
@RequestMapping("api")
public class TEquipmentController {

    @Autowired
    private TEquipmentService tEquipmentService;

    @Autowired
    private TEqptModelService tEqptModelService;

    @Autowired
    private TCustomerInfoService tCustomerInfoService;

    @Log("查询TEquipment")
    @GetMapping(value = "/tEquipment")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_SELECT')")
    public ResponseEntity getTEquipments(TEquipmentQueryCriteria criteria, Pageable pageable){
        try {
            return new ResponseEntity(tEquipmentService.queryAll(criteria, pageable), HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("查询TEqptModel")
    @GetMapping(value = "/tEquipment/model")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_SELECT')")
    public ResponseEntity getTEqptModels(TEqptModelQueryCriteria criteria, Pageable pageable){
        try {
            return new ResponseEntity(tEqptModelService.queryAll(criteria,pageable),HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("新增TEquipment")
    @PostMapping(value = "/tEquipment")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TEquipment resources){
        try {
            if(resources.getCustomerId() != null) {
                TCustomerInfoDTO customerInfo = tCustomerInfoService.findById(resources.getCustomerId());
                resources.setCustomerName(customerInfo.getCustomerName());
            }
            return new ResponseEntity(tEquipmentService.create(resources),HttpStatus.CREATED);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("新增TEqptModel")
    @PostMapping(value = "/tEquipment/model")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_CREATE')")
    public ResponseEntity createModel(@Validated @RequestBody TEqptModel resources){
        try {
            return new ResponseEntity(tEqptModelService.create(resources),HttpStatus.CREATED);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("修改TEquipment")
    @PutMapping(value = "/tEquipment")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TEquipment resources){
        try {
            if(resources.getCustomerId() != null) {
                TCustomerInfoDTO customerInfo = tCustomerInfoService.findById(resources.getCustomerId());
                resources.setCustomerName(customerInfo.getCustomerName());
            }

            tEquipmentService.update(resources);
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("修改TEqptModel")
    @PutMapping(value = "/tEquipment/model")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_EDIT')")
    public ResponseEntity updateModel(@Validated @RequestBody TEqptModel resources){
        try {
            tEqptModelService.update(resources);
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @Log("删除TEquipment")
    @DeleteMapping(value = "/tEquipment/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id){
        try {
            tEquipmentService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("删除TEqptModel")
    @DeleteMapping(value = "/tEquipment/model/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','TEQUIPMENT_ALL','TEQUIPMENT_DELETE')")
    public ResponseEntity deleteModel(@PathVariable Integer id){
        try {
            tEqptModelService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("根据客户id查询")
    @GetMapping(value = "/tEquipment/byCustomerId/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','DICT_ALL','DICT_SELECT')")
    public ResponseEntity getEqByCustomerId(@PathVariable int  id){
        try {
            return new ResponseEntity(tEquipmentService.getEqByCustomerId(id), HttpStatus.OK);
        }catch(Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}